package com.gzw.scm.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.gzw.scm.MyApplication;
import com.gzw.scm.R;
import com.gzw.scm.bean.Constant;
import com.gzw.scm.bean.UserInfo;
import com.zhy.autolayout.config.AutoLayoutConifg;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AutoLayoutConifg.getInstance().useDeviceSize();

        setContentView(R.layout.activity_splash);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        new MyHandler().sendEmptyMessageDelayed(0, 2000);
    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initData();
        }
    }

    public void initData() {
        SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY,
                Context.MODE_PRIVATE);
        UserInfo userInfo = new UserInfo();
        userInfo.setPassword(sharedPreferences.getString("passWord", ""));
        userInfo.setUsername(sharedPreferences.getString(Constant.USER_NAME_KEY, ""));
        userInfo.setId(Integer.valueOf(sharedPreferences.getInt(Constant.UAER_ID,000000)));
        userInfo.setName(sharedPreferences.getString(Constant.NAME_KEY, ""));
        userInfo.setDepartment_name(sharedPreferences.getString(Constant.DEPARTMENT_NAME, ""));
        userInfo.setDid(sharedPreferences.getInt(Constant.DID_KEY,0));
        userInfo.setDepartmentType(sharedPreferences.getInt(Constant.DEPARTMENTTYPE,100));
        userInfo.setBelongId(sharedPreferences.getInt(Constant.BELONGID,0));
        MyApplication.setIsUserLogin(sharedPreferences.getBoolean(Constant.IS_USER_LOGIN_KEY, false));
        MyApplication.setIsRemember(sharedPreferences.getBoolean(Constant.REMEMBER, false));

        MyApplication.setFlag(1);
        MyApplication.setUserInfo(userInfo);


        Intent mIntent = new Intent();
        if (MyApplication.getIsUserLogin()) {
                mIntent.setClass(this, MainActivity.class);
            mIntent.putExtra("DepartmentType",userInfo.getDepartmentType());
            startActivity(mIntent);
            finish();
        } else {
            mIntent.setClass(this, LoginActivity.class);
            startActivity(mIntent);
            finish();
        }
    }

}
